package com.alongstudio.account.action;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alongstudio.account.action.base.BaseAction;
import com.alongstudio.account.commons.exception.BusinessException;
import com.alongstudio.account.constants.DictionaryCodeConstant;
import com.alongstudio.account.constants.DictionaryCodeEnum;
import com.alongstudio.account.constants.IViewConstant;
import com.alongstudio.account.entity.Account;
import com.alongstudio.account.entity.ClientUser;
import com.alongstudio.account.entity.DictionaryContent;
import com.alongstudio.account.service.IAccountService;
import com.alongstudio.account.service.IDictionaryService;

@SuppressWarnings("serial")
@Component("AccountsAction")
@Scope("prototype")
public class AccountsAction extends BaseAction {
	private Account account;
	private List<Account> accountList;
	@Autowired
	private IAccountService accountService;
	@Autowired
	private IDictionaryService dictService;
	private String className = AccountsAction.class.getName();
	private String id;
	private List<DictionaryContent> currencyList;
	private Byte type;

	@Override
	public String executeAction() throws BusinessException {
		return SUCCESS;
	}

	/**
	 * 显示新增账户对话框
	 * 
	 * @return
	 * @throws BusinessException
	 */
	public String showNewAccountDialog() throws BusinessException {
		return getCurrency();
	}

	/**
	 * 显示编辑账户对话框
	 * 
	 * @return
	 * @throws BusinessException
	 */
	public String showEditAccountDialog() throws BusinessException {
		return getCurrency();
	}

	/**
	 * 获取币种
	 * 
	 * @return
	 * @throws BusinessException
	 */
	private String getCurrency() throws BusinessException {
		try {
			currencyList = dictService.findDictCotent(DictionaryCodeConstant
					.getDictoryCode(DictionaryCodeEnum.CURRENCY));
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return SUCCESS;
	}

	/**
	 * 删除账户
	 * 
	 * @return
	 * @throws BusinessException
	 */
	public String deleteAccount() throws BusinessException {
		boolean isOk = false;
		try {
			if (id != null) {
				isOk = accountService.deleteAccount(id);
			}
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		setOPERATOR_RESULT( isOk ? IViewConstant.DELETE_SUCCESS : IViewConstant.DELETE_FAILED);
		return SUCCESS;
	}

	/**
	 * 根据id查找
	 * 
	 * @return
	 * @throws BusinessException
	 */
	public String getAccountById() throws BusinessException {
		try {
			if (id != null) {
				account = accountService.findById(id);
			}
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return SUCCESS;
	}

	/**
	 * 修改账户信息
	 * 
	 * @return
	 * @throws BusinessException
	 */
	public String updateAccount() throws BusinessException {
		try {
			boolean isOk = false;
			if (account != null && account.getId() != null) {
				account.setUpdateTime(new Date());
				isOk = accountService.updateAccount(account);
			}
			setOPERATOR_RESULT(isOk ? IViewConstant.SAVE_SUCCESS : IViewConstant.SAVE_FAILED);
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return SUCCESS;
	}


	/**
	 * 获取账户
	 */
	public String getAccountData() throws BusinessException {
		try {
			accountList = accountService.findAccounts(getCurrentClientUserId(), type);
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return SUCCESS;
	}

	public String newAccount() throws BusinessException {
		try {
			if (account != null) {
				account.setCreateTime(new Date());
				ClientUser clientUser = new ClientUser();
				clientUser.setId(getCurrentClientUserId());
				account.setClientUser(clientUser);
				String id = accountService.createAccount(account);
				setOPERATOR_RESULT((id != null && !id.isEmpty()) ? IViewConstant.SAVE_SUCCESS : IViewConstant.SAVE_FAILED);
			}
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return SUCCESS;

	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public List<Account> getAccountList() {
		return accountList;
	}

	public void setAccountList(List<Account> accountList) {
		this.accountList = accountList;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<DictionaryContent> getCurrencyList() {
		return currencyList;
	}

	public void setCurrencyList(List<DictionaryContent> currencyList) {
		this.currencyList = currencyList;
	}

	public Byte getType() {
		return type;
	}

	public void setType(Byte type) {
		this.type = type;
	}

}
